Optimize think tag parser with early-exit lastIndexOf and cursor indexing - #1219
Optimize think tag parser with early-exit lastIndexOf and cursor indexing#1219nordicnode wants to merge 1 commit into
Conversation
|
Nice work here. The core insight — deriving partial-tag detection from I traced through the The Two things worth double-checking before this gets ported:
Overall this is a tight, single-purpose, tested diff with no behavior change — a reasonable candidate to port after re-verifying against the private tree's tag constants. |
Optimize think tag parser with early-exit lastIndexOf and cursor indexing
Summary
• In$O(1)$ check using
cli/src/utils/think-tag-parser.ts, optimize streaming think-tag detection and segment parsing.• Previously,
getPartialTagLengthwas called on every streamed token chunk and sequentially tested 12 hardcoded prefix strings acrossPARTIAL_CLOSE_PREFIXESandPARTIAL_OPEN_PREFIXESwithtext.endsWith(prefix). Replaced this with antext.lastIndexOf('<'). If<does not appear within the last 7 characters of the string (since the longest partial prefix is'</think', length 7), it returns 0 immediately without evaluating prefix arrays. If<is found in the trailing window, it verifies the prefix against the constant tags.• Measured
getPartialTagLength:• In
parseThinkTags, replaced continuousremaining = remaining.slice(...)string allocations with an index cursorlet cursor = 0to scan tags directly on the source string without allocating intermediate substrings.• Removed unused
PARTIAL_OPEN_PREFIXESandPARTIAL_CLOSE_PREFIXESarrays.• Verified 100% behavioral and edge-case parity against all 22 existing unit tests.
Test plan
[✓]
bun test --config=/dev/null src/utils/__tests__/think-tag-parser.test.ts— 22 pass, 0 fail[✓]
bun run --cwd cli typecheck— 0 errors[✓] PR hygiene check passed